﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.Models;
using System.Configuration;
using KMF.Utility;
namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminCustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var model = CustomerDb.GetAllCustomerPaging(page, itemsPerPage, ref totalPage) ?? new List<CustomerPagingDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            return View(model);
        }
        public ActionResult Add()
        {
            var model = new CustomerDto();
            return View(model);
        }
        [HttpPost]
        public ActionResult Add(CustomerDto model)
        {
            model.Id = Utility.Common.GenerateId();
            var passwordSalt = Guid.NewGuid().ToString();
            var password = model.Password.CreatePasswordHash(passwordSalt);
            model.Fax = "";
            model.CreateDate = DateTime.Now;
            model.Password = password;
            model.PasswordSalt = passwordSalt;
            model.CompanyAddress = "";
            model.CompanyPhone = "";
            model.ProvinceId = 0;
            model.DistrictId = 0;
            model.CountryId = 0;
            model.ActivateCode = "";
            model.EmailStatus = false;

            var result = CustomerDb.Add(model);
            if (result > 0)
                return RedirectToAction("Index", "AdminCustomer");
            return View(model);
            
        }

        public ActionResult Edit(string id)
        {
            var model = CustomerDb.GetById(id) ?? new CustomerDto();
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(CustomerDto model)
        {
            //var passwordSalt = Guid.NewGuid().ToString();
            //if (!string.IsNullOrEmpty(model.Password))
            //{
            //    var password = model.Password.CreatePasswordHash(model.PasswordSalt);
            //    model.Password = password;
            //}
            model.Fax = "";
            model.CreateDate = DateTime.Now;
            model.CompanyAddress = "";
            model.CompanyPhone = "";
            model.ProvinceId = 0;
            model.DistrictId = 0;
            model.CountryId = 0;
            model.ActivateCode = "";
            model.EmailStatus = false;

            CustomerDb.Update(model);
            return RedirectToAction("Index", "AdminCustomer");
        }
        public JsonResult DeleteCustomer(string cusId)
        {
            var data = 0;
            if (!string.IsNullOrEmpty(cusId))
            {
                data = CustomerDb.Delete(cusId);
            }
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SearchCustomer(string keyword, int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var model = new List<CustomerPagingDto>();
            if(!string.IsNullOrEmpty(keyword)){
                model = CustomerDb.GetAllCustomerPagingByKeyword(page, itemsPerPage, ref totalPage, keyword) ?? new List<CustomerPagingDto>();
            }
            else
            {
                model = CustomerDb.GetAllCustomerPaging(page, itemsPerPage, ref totalPage) ?? new List<CustomerPagingDto>();
            }
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.Keyword = keyword;
            return View(model);
        }
        public JsonResult ChangePass(string id, string passOld, string passnew)
        {
            var Customer = CustomerDb.GetById(id);
            var data = false;
            if (Customer != null)
            {
                if (Customer.Password == passOld.CreatePasswordHash(Customer.PasswordSalt))
                {
                    Customer.Password = passnew.CreatePasswordHash(Customer.PasswordSalt);
                    data = true;
                }
            }
            CustomerDb.Update(Customer);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
